package cn.tedu.cgb2110boot02.response;
/**Servlet 充当服务器:接受请求Request + 做出相应 Response*/
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/resp/servlet01")
public class response01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解析参数
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        System.out.println(id+name);
        //响应时使用的是默认的字符集ios-8859-1,为了防止响应时的中文乱码问题
        response.setContentType("text/html;charset=utf8");
        //响应解析到的数据
        //字符输出流
//        PrintWriter out = response.getWriter();
//        out.write(id);
//        out.write(name);//如果不传数据，会报空指针异常
//        out.print(id);//如果不传数据，不会报错


        //字节输出流
        ServletOutputStream outputStream = response.getOutputStream();
        //字节流只能写出字节int byte[] ,getBytes 方法  作用：使String类型转成 Byte[] 数组
        outputStream.write(id.getBytes());
        outputStream.write(name.getBytes());
        outputStream.print(id);

        //获取字符流--写出数据
//        String id1 = request.getParameter("id");
//        String name1 = request.getParameter("name");
//        String json = "{\"id\":\""+id1+"\",\"name\":\""+name1+"\"}";
//        out.println(json);
    }

}
